﻿using FineUI;
using System;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.PartyVolunteer
{
    public partial class PartyVolunteerActivityEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion

        #region 页面属性
        public string EditorContent
        {
            get
            {
                string id = GetQueryIntValue("id");
                if (!string.IsNullOrEmpty(id))
                {
                    PartyVolunteerActivitie article = DB.PartyVolunteerActivities.Find(id);
                    return article.Summary;
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(ddlThematic, "ZHDJ.BranchActivity.Thematic");
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyVolunteerActivitie activity = DB.PartyVolunteerActivities.Find(id);
                if (activity == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = activity.Title;
                ddlCategory.SelectedValue = activity.Category;
                ddlThematic.SelectedValue = activity.Thematic;
                dpBeginTime.Text = Convert.ToDateTime(activity.BeginTime).ToString("yyyy-MM-dd");
                dpEndTime.Text = Convert.ToDateTime(activity.EndTime).ToString("yyyy-MM-dd");
                txtAddress.Text = activity.Address;
                hdFileID.Text = activity.FileID;
                ddlIsShowHome.SelectedValue = activity.IsShowHome ? "1" : "0";
                txtCreateBy.Text = this.CurrentUser.UserName;
                txtBranch.Text = ControlBusiness.GetBranchPickerText(this.BranchID);

                IQueryable<PartyVolunteerActivitieMember> q = DB.PartyVolunteerActivitieMembers.Where(t => t.ActivitieID == id);

                if (q.Count() > 0)
                {
                    StringBuilder sbLoginID = new StringBuilder();
                    StringBuilder sbName = new StringBuilder();

                    foreach (PartyVolunteerActivitieMember item in q)
                    {
                        sbLoginID.Append(item.LoginID + ",");
                        sbName.Append(ControlBusiness.GetMembersPickerText(item.LoginID) + ",");
                    }

                    ppMember.Value = sbLoginID.ToString().TrimEnd(',');
                    ppMember.Text = sbName.ToString().TrimEnd(',');
                }

                if (activity.Status == 2)
                {
                    btnSaveClose.Enabled = btnPublish.Enabled = false;
                }
            }
            else
            {
                txtCreateBy.Text = this.CurrentUser.UserName;
                txtBranch.Text = ControlBusiness.GetBranchPickerText(this.BranchID);
            }
        }
        #endregion

        #region Events

        private void SaveData(bool isPublish)
        {
            string id = GetQueryIntValue("id");
            string _id = id;

            if (id != "")
            {
                //更新
                PartyVolunteerActivitie activity = DB.PartyVolunteerActivities.Find(id);
                if (activity == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                activity.Title = txtTitle.Text.Trim();
                activity.Category = ddlCategory.SelectedValue;
                activity.Thematic = ddlThematic.SelectedValue;
                activity.BeginTime = Convert.ToDateTime(dpBeginTime.Text);
                activity.EndTime = Convert.ToDateTime(dpEndTime.Text);
                activity.Address = txtAddress.Text.Trim();
                activity.FileID = hdFileID.Text;
                activity.Status = isPublish ? 2 : 1;
                activity.IsShowHome = ddlIsShowHome.SelectedValue == "1";
                activity.Summary = this.Request.Form["editorValue"];
                activity.ModifyTime = DateTime.Now;
                activity.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                PartyVolunteerActivitie activity = new PartyVolunteerActivitie();
                activity.ID = StringHelper.NewID;
                _id = activity.ID;
                activity.Title = txtTitle.Text.Trim();
                activity.Category = ddlCategory.SelectedValue;
                activity.BranchID = this.BranchID;
                activity.BeginTime = Convert.ToDateTime(dpBeginTime.Text);
                activity.EndTime = Convert.ToDateTime(dpEndTime.Text);
                activity.Address = txtAddress.Text.Trim();
                activity.Thematic = ddlThematic.SelectedValue;
                activity.IsShowHome = ddlIsShowHome.SelectedValue == "1";
                activity.FileID = hdFileID.Text;
                activity.Summary = this.Request.Form["editorValue"];
                activity.Status = isPublish ? 2 : 1;
                activity.IsEnd = false;
                activity.CreateTime = DateTime.Now;
                activity.ModifyTime = DateTime.Now;
                activity.CreateBy = CurrentUser.LoginID;
                activity.ModifyBy = CurrentUser.LoginID;

                DB.PartyVolunteerActivities.Add(activity);

                DB.SaveChanges();
            }

            DB.PartyVolunteerActivitieMembers.Where(t => t.ActivitieID == _id).ToList().ForEach(t => DB.PartyVolunteerActivitieMembers.Remove(t));

            if (!string.IsNullOrEmpty(ppMember.Value))
            {
                PartyVolunteerActivitieMember member = null;
                foreach (string item in ppMember.Value.Split(','))
                {
                    member = new PartyVolunteerActivitieMember();
                    member.ID = StringHelper.NewID;
                    member.ActivitieID = _id;
                    member.LoginID = item;
                    member.Status = 1;

                    DB.PartyVolunteerActivitieMembers.Add(member);

                    DB.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(false);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnPublish_Click(object sender, EventArgs e)
        {
            SaveData(true);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void fileupload_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(fileupload, CurrentUser.LoginID);
            if (rs.IsSuccess)
            {
                hdFileID.Text = rs.FileID;
            }
        }
        #endregion
    }
}